package com.telecomyt.jwportal.base;


import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.squareup.leakcanary.RefWatcher;
import com.telecomyt.jwportal.BaseApplication;
import com.telecomyt.jwportal.R;
import com.telecomyt.jwportal.base.i.IBaseView;
import com.telecomyt.jwportal.service.CameraService;

import butterknife.ButterKnife;

public abstract class BaseActivity<P extends BasePresenter> extends FraBaseActivity implements IBaseView {
    private static final String TAG = "================";
    protected P persenter;
    private ProgressDialog progressDialog;
    private View mTipView;

    private WindowManager mWindowManager;

    private WindowManager.LayoutParams mLayoutParams;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        persenter = creatPresenter();

        if (persenter != null) {
            persenter.attacView(this);
        }
        progressDialog = new ProgressDialog(this);
//        progressDialog.setIcon(R.mipmap.ic_launcher);
//        progressDialog.setTitle("加载dialog");
        progressDialog.setMessage("加载中...");
        progressDialog.setIndeterminate(true);// 是否形成一个加载动画  true表示不明确加载进度形成转圈动画  false 表示明确加载进度
        progressDialog.setCancelable(false);//点击返回键或者dialog四周是否关闭dialog  true表示可以关闭 false表示不可关闭
        initTipView();//初始化提示view
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (persenter != null) {
            persenter.detachView();
            persenter = null;
        }
        RefWatcher refWatcher = BaseApplication.getRefWatcher(this);
        refWatcher.watch(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        initView();
        initData();
    }

    private void initTipView() {
        LayoutInflater inflater = getLayoutInflater();

        mTipView = inflater.inflate(R.layout.layout_network_tip, null); //提示View布局

        mWindowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);

        mLayoutParams = new WindowManager.LayoutParams(

                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,

                WindowManager.LayoutParams.TYPE_APPLICATION,

                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE

                        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,

                PixelFormat.TRANSLUCENT);

//使用非CENTER时，可以通过设置XY的值来改变View的位置

        mLayoutParams.gravity = Gravity.TOP;

        mLayoutParams.x = 0;

        mLayoutParams.y = 0;

    }


    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract P creatPresenter();

    /**
     * IbaseView 公共方法 可显示网络请求dialog
     */
    @Override
    public void showLoading() {
        if (progressDialog != null) {
            progressDialog.show();
        }
    }

    @Override
    public void dismissLoading() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    @Override
    public void onError() {

    }

    @Override
    public Context getContext() {
        return this;
    }

    /**
     * 隐藏悬浮窗
     */
    public void dismissFloatingView() {
        if (CameraService.isStart) {
            LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(CameraService.ACTION_DISMISS_FLOATING));
        }
    }

    /**
     * 显示悬浮窗
     */
    public void showFloatingView() {
        if (CameraService.isStart) {
            //通知显示悬浮窗
            LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(CameraService.ACTION_SHOW_FLOATING));
        } else {
            //启动悬浮窗管理服务
            startService(new Intent(this, CameraService.class));
        }
    }

    public void showNetWork(boolean isFlag) {
        if (isFlag) {
            mWindowManager.addView(mTipView, mLayoutParams);
        } else {
            mWindowManager.removeView(mTipView);
        }

    }

    @Override
    public void finish() {
        super.finish();
        //当提示View被动态添加后直接关闭页面会导致该View内存溢出，所以需要在finish时移除
        if (mTipView != null && mTipView.getParent() != null) {
            mWindowManager.removeView(mTipView);
        }
    }
}
